package com.bianmaba.dc.task;

import com.bianmaba.dc.bean.CollectionData;
import com.bianmaba.dc.bean.CollectionRequest;
import com.bianmaba.dc.bean.InterfaceType;
import com.bianmaba.dc.bean.TaskStatus;
import com.bianmaba.dc.supports.services.AbstractCollectionService;
import com.bianmaba.dc.supports.services.DefaultCollectionService;
import lombok.Data;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 采集器任务，此类放在任务执行器中异步执行，如果不是必要，不需要重写此类
 */
@Data
@Log4j2
public class CollectionTask implements Runnable {
    LinkedBlockingQueue<String> logs = new LinkedBlockingQueue<>();
    private TaskStatus status = TaskStatus.RUNNING;
    private Object datas;
    private CollectionRequest collectionRequest;
    @Autowired
    private AbstractCollectionService service;

    public CollectionTask(CollectionRequest collectionRequest) {
        this.collectionRequest = collectionRequest;
    }

    //添加一条日志
    public void putLog(String log) {
        logs.offer(log);
    }

    //弹出所有日志
    public List<String> pollLogs() {
        List<String> list = new ArrayList<>();
        String line;
        while ((line = logs.poll()) != null) {
            list.add(line);
        }
        return list;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(1000);
            CollectionData datas = collect(this.collectionRequest);
            setDatas(datas);
            setStatus(TaskStatus.SUCCESSFUL);
        } catch (InterruptedException e) {
            setStatus(TaskStatus.USER_TERMINATED);
            putLog("采集器：用户终止");
        } catch (Exception e) {
            setStatus(TaskStatus.EXCEPTIONAL);
            putLog("采集器：采集任务运行异常：" + e.getLocalizedMessage());
        }
    }


    /**
     * 数据采信函数，此函数返回多行数据，每行数据用Map封闭
     *
     * @param request
     * @return
     * @throws Exception
     */

    protected CollectionData collect(CollectionRequest request) throws Exception {
        CollectionData datas = CollectionData.of(request.getProjectId(), request.getTaskId(), request.getTaskInstanceId(), InterfaceType.DTS);
        if (service.getClass().getName().equalsIgnoreCase(DefaultCollectionService.class.getName())) {
            log.warn("采集器未实现采集服务类“AbstractCollectionService”，将返回空数据。");
        }
        CollectionData.Table proejctStatisticsTable = service.collectionProjectStatistics(request);
        if (proejctStatisticsTable != null) {
            datas.addTable(proejctStatisticsTable);
            Thread.sleep(1);
        }
        CollectionData.Table workforceStatisticsTable = service.collectionWorkforceStatistics(request);
        if (workforceStatisticsTable != null) {
            datas.addTable(workforceStatisticsTable);
            Thread.sleep(1);
        }

        CollectionData.Table demandStatisticsTable = service.collectionDemandStatistics(request);
        if (demandStatisticsTable != null) {
            datas.addTable(demandStatisticsTable);
            Thread.sleep(1);
        }
        CollectionData.Table sourceDataTable = service.collectionSourceData(request);
        if (sourceDataTable != null) {
            datas.addTable(sourceDataTable);
        }
        return datas;
    }

}
